<?php
namespace addons\fastworker\example;

use addons\fastworker\library\Server as WorkerServer;

class Server extends WorkerServer
{

    /**
     * 此处可修改gateway 协议
     * @var string
     */
    protected $protocol = 'websocket';
    protected $host     = '0.0.0.0';
    protected $port     = '2346';
    protected $option   = [
        // 设置参数 如线程数等
        'count'  => 2,
    ];


    /**
     * 支持回调函数 onWorkerStart, onConnect, onMessage, onClose, onError, onBufferFull, onBufferDrain, onWorkerReload
     * 在回调函数中完成需要的业务逻辑
     */


    /**
     * Worker子进程启动时的回调函数,每个子进程启动时都会执行
     * @param $worker Worker对象
     */
    public  function onWorkerStart($worker){

        echo "Worker starting...\n";

    }

    /**
     * Worker收到reload信号后执行的回调(不常用到)
     * @param $worker
     */
    public function onWorkerReload($worker){

        // 执行reload后告诉所有客户端服务端执行了reload
        foreach($worker->connections as $connection)
        {
            $connection->send('worker reloading');
        }

    }

    /**
     * 当客户端与Workerman建立连接时(TCP三次握手完成后)触发的回调函数
     * @param $connection TcpConnection实例
     */
    public function onConnect($connection){

        //输出ip地址和端口
        echo "new connection from address " . $connection->getRemoteIp() . ":". $connection->getRemotePort() ."\n";
        //  给connection对象动态添加一个属性，记录收到的消息数量
        $connection->messageCount = 0;
        // 设置当前连接发送缓冲区，单位字节
        $connection->maxSendBufferSize = 102400;
        $totalConnections = count($this->worker->connections);
        foreach($this->worker->connections as $connection)
        {
            $connection->send('当前连接数:'.$totalConnections);
        }

    }

    /**
     * 当客户端通过连接发来数据时(Workerman收到数据时)触发的回调函数
     * @param $connection
     * @param $data  收到的数据
     */
    public function onMessage($connection,$data)
    {

        if(++$connection->messageCount > 2) {
            //停止接受消息
            $connection->pauseRecv();
            $connection->send('发送消息过于频繁');

            // 添加个计时器，10秒后恢复接收数据
            \Workerman\Lib\Timer::add(10, function($connection){
                $connection->messageCount = 0;
                $connection->resumeRecv();
            }, array($connection), false);
        }else{
            $connection->send("收到第{$connection->messageCount}条消息:".json_encode($data,JSON_UNESCAPED_UNICODE));
        }


    }

    /**
     * 当客户端连接与Workerman断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection){

        echo "connection closed\n";

    }

    /**
     * 发送缓冲区满时触发的回调函数
     * @doc  http://doc.workerman.net/worker/on-buffer-full.html
     * @param $connection
     */
    public function onBufferFull($connection){

        echo "bufferFull {$connection->maxSendBufferSize} bytes and do not send again\n";

    }

    /**
     * 应用层发送缓冲区数据全部发送完毕后触发的回调函数
     * @param $connection
     */
    public function onBufferDrain ($connection){

        echo "buffer drain and continue send\n";

    }


    /**
     * 当客户端的连接上发生错误时触发的回调函数
     * @doc http://doc.workerman.net/worker/on-error.html
     * @param $connection
     * @param $code  错误码
     * @param $msg   错误消息
     */
    public function onError($connection, $code, $msg){

        echo "error $code $msg\n";

    }
}